package com.example.xln111.Dao;

import com.example.xln111.model.Grade;
import com.example.xln111.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GradeDao {
        @Autowired
        private JdbcTemplate jdbcTemplate;

        public List<Grade> getAll(){
            return jdbcTemplate.query("select * from grade",new BeanPropertyRowMapper<>(Grade.class));
        }

        public int addGrade(Grade grade){
            return jdbcTemplate.update("INSERT INTO grade(studentId,courseId,time,score) VALUES(?,?,?,?)",grade.getStudentId(),grade.getCourseId(),grade.getTime(),grade.getScore());
        }

        public int updateGrade(Grade grade){
            return jdbcTemplate.update("UPDATE grade SET courseId=?,time=?,score=? WHERE studentId=?",
                    grade.getCourseId(),grade.getTime(),grade.getScore());
        }

        public int deleteGradeById(int studentId){
            return jdbcTemplate.update("DELETE FROM grade WHERE studentId=?",studentId);
        }

        public Grade getGradeById(int studentId) {
            return jdbcTemplate.queryForObject("select * from grade where studentId = ?", new BeanPropertyRowMapper<>(Grade.class), studentId);
        }
    }


